package com.wb.mas.ui.refund;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.wb.mas.BR;
import com.wb.mas.R;
import com.wb.mas.app.Constants;
import com.wb.mas.data.DemoRepository;
import com.wb.mas.entity.CodeEntity;
import com.wb.mas.entity.PayCodeResponse;
import com.wb.mas.entity.PayEntity;
import com.wb.mas.ui.base.viewmodel.ToolbarViewModel;
import com.wb.mas.ui.webview.WebViewActivity;
import com.wb.mas.utils.MoneyUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.utils.KLog;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * Copyright (C) 2018,
 * OrderViewModel
 * <p>
 * Description
 * 还款
 *
 * @author mwl
 * @version 1.0
 * <p>
 * Ver 1.0, 2019-04-30, mwl, Create file
 */
public class RefundViewModel extends ToolbarViewModel {

    public String orderId;

    public String approvedAmt;

    public int type;

    public String selectCode = getString(R.string.refund_mode_atm);

    public List<PayEntity> payEntities = new ArrayList<>();


    public RefundViewModel(@NonNull Application application, DemoRepository model) {
        super(application, model);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
        getDatas();
    }

    private void init() {
        setTitleText(getString(R.string.refund_title));
        setRightIconVisible(View.GONE);
        borrowMoney.set(MoneyUtils.formatShow(approvedAmt));
    }


    //给RecyclerView添加ObservableList
    public ObservableList<RefundItemViewModel> observableList = new ObservableArrayList<>();

    /**
     * 借款金额显示
     */
    public ObservableField<String> borrowMoney = new ObservableField<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<RefundItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.recycle_refund);

    /**
     * 重新获取还款码事件
     */
    public BindingCommand reObtainOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getDatas();

        }
    });

    /**
     * 还款方式切换事件
     */
    public BindingCommand radioChangeCommand = new BindingCommand(new BindingConsumer<String>() {
        @Override
        public void call(String string) {
            selectCode = string;
            selectMode();
        }
    });


    /**
     * 还款指引点击事件
     */
    public BindingCommand guideOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle=new Bundle();
            bundle.putString("title",getString(R.string.refund_guide));
            bundle.putString("url", Constants.PAY_GUIDE_URL);
            startActivity(WebViewActivity.class,bundle);

        }
    });


    private void getDatas() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        if (type==RefundActivity.TYPE_REFUND){
            addSubscribe(model.getPayCode(map).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<PayCodeResponse>() {
                        @Override
                        public void accept(PayCodeResponse payCodeResponse) throws Exception {
                            dismissDialog();
                            payEntities.clear();
                            if (payCodeResponse != null && payCodeResponse.getData() != null) {
                                payEntities.addAll(payCodeResponse.getData().payCode);
                            }
                            selectMode();
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            dismissDialog();
                            KLog.w(throwable.getMessage());
                        }
                    }));
        }else{
            addSubscribe(model.getDeferPayCode(map).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<PayCodeResponse>() {
                        @Override
                        public void accept(PayCodeResponse payCodeResponse) throws Exception {
                            dismissDialog();
                            payEntities.clear();
                            if (payCodeResponse != null && payCodeResponse.getData() != null) {
                                payEntities.addAll(payCodeResponse.getData().payCode);
                            }
                            selectMode();
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            dismissDialog();
                            KLog.w(throwable.getMessage());
                        }
                    }));
        }

    }

    private void selectMode() {
        observableList.clear();
        for (PayEntity payEntity : payEntities) {
            if (getString(R.string.refund_mode_atm).equals(selectCode)) {
                if (!TextUtils.isEmpty(payEntity.permataCode)) {
                    CodeEntity codeEntity = new CodeEntity("PERMATA", payEntity.permataCode);
                    RefundItemViewModel itemViewModel = new RefundItemViewModel(this, codeEntity);
                    observableList.add(itemViewModel);
                }

                if (!TextUtils.isEmpty(payEntity.bniCode)) {
                    CodeEntity codeEntity = new CodeEntity("BNI", payEntity.bniCode);
                    RefundItemViewModel itemViewModel = new RefundItemViewModel(this, codeEntity);
                    observableList.add(itemViewModel);
                }

            } else {
                if (!TextUtils.isEmpty(payEntity.otcCode)) {
                    CodeEntity codeEntity = new CodeEntity("OTC", payEntity.otcCode);
                    RefundItemViewModel itemViewModel = new RefundItemViewModel(this, codeEntity);
                    observableList.add(itemViewModel);
                }
            }

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
